<%-- 
    Document   : exam-do-test-action
    Created on : Feb 22, 2012, 11:36:54 AM
    Author     : PhucAn
--%>

<%@page import="util.SharingUtil"%>
<%@page import="dto.Answer"%>
<%@page import="dto.Question"%>
<%@page import="dao.AnswerDAO"%>
<%@page import="dao.QuestionSetDAO"%>
<%@page import="dto.Exam"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.ExamDAO"%>
<%
    int id = Integer.parseInt(request.getParameter("id"));
    ExamDAO daoExam = new ExamDAO();
    QuestionSetDAO daoQS = new QuestionSetDAO();
    AnswerDAO daoAns = new AnswerDAO();
    ArrayList<Answer> ansList;
    Exam e = new Exam();
    e = daoExam.getExam(id);
    e.setQuestionSet(daoQS.getQuestions(id));
    ansList = daoAns.getAnswers(e);
    //TODO: chua toi uu, cai tien: rut ngan vong lap
    for (Question q : e.getQuestionSet()) {
        for (Answer ans : ansList) {
            if (ans.getQuestionId() == q.getId()) {
                q.getAnswers().add(ans);
                // ansList.remove(ans);
            }
        }
    }
    //process event click button Submit
    if (request.getParameter("submit-result") != null) {
        String[] answers;
        int currentQuestionId = 0;
        int questionId = 0;
        int currentAnswerId = 0;
        //Question currentQuestion = new Question();
        for (Question q : e.getQuestionSet()) {
            questionId = q.getId();
            answers = request.getParameterValues("question-answer-" + questionId);
            if (answers != null) {
                for (String ans : answers) {
                    currentAnswerId = Integer.parseInt(ans);
                    for (Answer answer : q.getAnswers()) {
                        //set if this answer is chose by student
                        if (answer.getId() == currentAnswerId) {
                            answer.setIsUserChoice(true);
                            break;
                        }
                    }
                }
            }
        }
        /*
        for (String ans : answers) {
        String[] info = ans.split("-");
        questionId = Integer.parseInt(info[0]);
        currentAnswerId = Integer.parseInt(info[1]);
        if (questionId != currentQuestionId) {
        currentQuestionId = questionId;
        }
        for (Question q : e.getQuestionSet()) {
        if (q.getId() == currentQuestionId) {
        currentQuestion = q;
        break;
        }
        }
        for (Answer answer : currentQuestion.getAnswers()) {
        if (answer.getId() == currentAnswerId) {
        answer.setIsUserChoice(true);
        }
        }
        }
         */
        float totalMark = 0;
        for (Question q1 : e.getQuestionSet()) {
            if (q1.checkAnswer()) {
                totalMark += q1.getMark();
            }
        }
        request.setAttribute("mark", totalMark);
         request.setAttribute("totalMark", e.getTotalMark());
        request.setAttribute("exam", e);
        request.setAttribute("student", SharingUtil.getUser(session));
        RequestDispatcher rd = application.getRequestDispatcher("/public/index.jsp?pcontent=exam-result");
        rd.forward(request, response);
       
    }
    request.setAttribute("exam", e);
%>
